package com.jeesite.modules.web;

import com.jeesite.common.web.BaseController;
import com.jeesite.modules.service.DonationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公开的捐赠信息Controller（无需登录）
 * @author KBB Team
 * @version 2025-08-18
 */
@RestController
@RequestMapping(value = "${adminPath}/my/donation")
public class PublicDonationController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(PublicDonationController.class);

    @Autowired
    private DonationService donationService;

    /**
     * 获取捐赠统计信息（公开访问）
     */
    @GetMapping("/statistics")
    public Map<String, Object> getStatistics() {
        try {
            Map<String, Object> statistics = donationService.getDonationStatistics();
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "统计信息获取成功");
            result.put("data", statistics);
            
            return result;
        } catch (Exception e) {
            logger.error("获取捐赠统计信息失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "获取统计信息失败: " + e.getMessage());
            result.put("data", null);
            return result;
        }
    }

    /**
     * 获取捐赠感谢名单（公开访问）
     */
    @GetMapping("/list")
    public Map<String, Object> getDonationList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Map<String, Object> thanksList = donationService.getThanksList(page, size);

            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "感谢名单获取成功");
            result.put("data", thanksList);

            return result;
        } catch (Exception e) {
            logger.error("获取感谢名单失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "获取感谢名单失败: " + e.getMessage());
            result.put("data", null);
            return result;
        }
    }

    /**
     * 获取捐赠排行榜（公开访问）
     */
    @GetMapping("/ranking")
    public Map<String, Object> getRanking(@RequestParam(defaultValue = "10") int limit) {
        try {
            List<Map<String, Object>> ranking = donationService.getDonationRanking(limit);

            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "排行榜获取成功");
            result.put("data", ranking);

            return result;
        } catch (Exception e) {
            logger.error("获取捐赠排行榜失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "获取排行榜失败: " + e.getMessage());
            result.put("data", null);
            return result;
        }
    }

    /**
     * 获取捐赠配置信息（公开访问）
     */
    @GetMapping("/config")
    public Map<String, Object> getConfig() {
        try {
            Map<String, Object> config = donationService.getDonationConfig();

            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "配置信息获取成功");
            result.put("data", config);

            return result;
        } catch (Exception e) {
            logger.error("获取捐赠配置失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "获取配置信息失败: " + e.getMessage());
            result.put("data", null);
            return result;
        }
    }

    /**
     * 调试接口：获取数据库中的实际记录情况
     */
    @GetMapping("/debug")
    public Map<String, Object> getDebugInfo() {
        try {
            Map<String, Object> debugInfo = donationService.getDebugInfo();

            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "调试信息获取成功");
            result.put("data", debugInfo);

            return result;
        } catch (Exception e) {
            logger.error("获取调试信息失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "获取调试信息失败: " + e.getMessage());
            result.put("data", null);
            return result;
        }
    }
}
